<%-- 
    Document   : LoginAction
    Created on : Nov 17, 2013, 12:36:46 PM
    Author     : smarzoug
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page language="java"%>
<%@ page import="ece.fr.edu.fac.utilisateur.modele.Profil,ece.fr.edu.fac.utilisateur.oad.SécuritéDAO"%>
<%
	String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>LoginAction.jsp</title>
    </head>
    <body>
        <%
			String target = "index.jsp?status=Invalid username and password";
				try{
				 Profil  rb=new Profil();
				 String username = request.getParameter("username");
		        System.out.println(request.getParameter("username"));
		        System.out.println(request.getParameter("password")); 
		        rb.setLoginID(username);
		         rb.setPassword(request.getParameter("password"));
		         
		         String role=new SécuritéDAO().loginCheck(rb);
		         System.out.println(role);
		         
		         if(role.equals("admin"))
		         { 
		            target = "AdminView.jsp?status=Welcome "+username;
		            session.setAttribute("user",username);
		            session.setAttribute("role",role);
		         } 
		         else if(role.equals("user"))
		         {
		            int status = new SécuritéDAO().checkFirstLogin(username);
		            System.out.println("this is status"+status);
		           if(status==0)
		            	target = "Student.jsp";
		            else if(status==1)
		                target = "StudentFrames.html?status=Welcome "+username;
		            else
		                target = "index.jsp?status=Invalid username and password";    	
		            session.setAttribute("user",username);
		            session.setAttribute("role",role);
		         } 
		         else  
		            target = "index.jsp?status=Invalid username and password";
		            }catch(Exception e){} 
		        response.sendRedirect(target);
		%>
    </body>
</html>
